﻿using App.Desk.UserControls;
using App.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App.Desk.Extend
{
    public class UIHelper
    {
        #region 委托 设置 UIDataGridView 绑定数据

        private delegate void UIBindGrid(UIDataGridView grid, List<ProSampling> source);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        public static void DoBindDataGrid(UIDataGridView grid, List<ProSampling> source)
        {
            if (grid != null && !grid.IsDisposed)
            {
                //在线程里以安全方式调用控件
                if (grid.InvokeRequired)
                {
                    UIBindGrid _MyInvoke = new UIBindGrid(DoBindDataGrid);
                    grid.Invoke(_MyInvoke, new object[] { grid, source });
                }
                else
                {
                    grid.DataSource = source;
                }
            }
        }

        #endregion

        #region 委托 设置 按钮可控状态

        /// <summary>
        /// 委托 设置 按钮可控状态
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        private delegate void SetButtonEnabled(UIButton control, bool value);

        /// <summary>
        /// 委托 设置 按钮可控状态
        /// </summary>
        public static void SetButtonEnabledControl(UIButton control, bool value)
        {
            try
            {
                //在线程里以安全方式调用控件
                if (!control.IsDisposed)
                {
                    if (control.InvokeRequired)
                    {
                        SetButtonEnabled myInvoke = SetButtonEnabledControl;
                        if (!control.IsDisposed)
                        {
                            control.Invoke(myInvoke, control, value);
                        }
                    }
                    else
                    {
                        control.Enabled = value;
                    }
                }
            }
            catch
            { }
        }

        #endregion

        #region 委托 设置 下拉框可控状态

        /// <summary>
        /// 委托 设置 下拉框可控状态
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        private delegate void SetCommboxEnabled(UIComboBox control, bool value);

        /// <summary>
        /// 委托 设置 下拉框可控状态
        /// </summary>
        public static void SetCommboxEnabledControl(UIComboBox control, bool value)
        {
            try
            {
                //在线程里以安全方式调用控件
                if (!control.IsDisposed)
                {
                    if (control.InvokeRequired)
                    {
                        SetCommboxEnabled myInvoke = SetCommboxEnabledControl;
                        if (!control.IsDisposed)
                        {
                            control.Invoke(myInvoke, control, value);
                        }
                    }
                    else
                    {
                        control.Enabled = value;
                    }
                }
            }
            catch
            { }
        }

        #endregion

        #region 线程 控制 状态 按钮

        /// <summary>
        /// 委托 设置 状态按钮显示
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        private delegate void SetLightState(UcStatusButton control, UILightState state);

        /// <summary>
        /// 状态按钮显示
        /// </summary>
        public static void SetLightControl(UcStatusButton control, UILightState state)
        {
            try
            {
                //在线程里以安全方式调用控件
                if (!control.IsDisposed)
                {
                    if (control.InvokeRequired)
                    {
                        SetLightState myInvoke = SetLightControl;
                        if (!control.IsDisposed)
                        {
                            control.Invoke(myInvoke, control, state);
                        }
                    }
                    else
                    {
                        control.SetState(state);
                    }
                }
            }
            catch
            { }
        }

        #endregion

        #region 线程 控制 状态 按钮

        /// <summary>
        /// 委托 设置 状态按钮显示
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        private delegate void SetInfoState(UcInfoButton control, string value);

        /// <summary>
        /// 状态按钮显示
        /// </summary>
        public static void SetInfoControl(UcInfoButton control, string value)
        {
            try
            {
                //在线程里以安全方式调用控件
                if (!control.IsDisposed)
                {
                    if (control.InvokeRequired)
                    {
                        SetInfoState myInvoke = SetInfoControl;
                        if (!control.IsDisposed)
                        {
                            control.Invoke(myInvoke, control, value);
                        }
                    }
                    else
                    {
                        if (control.GetValue() != value)
                        {
                            control.SetValue(value);
                        }
                    }
                }
            }
            catch
            { }
        }

        #endregion

        #region 线程 控制 状态 按钮

        /// <summary>
        /// 委托 设置 状态按钮显示
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        private delegate void SetUIFlowPanel(UIFlowLayoutPanel control, List<PailButton> values);

        /// <summary>
        /// 状态按钮显示
        /// </summary>
        public static void SetUIFlowPanelControl(UIFlowLayoutPanel control, List<PailButton> values)
        {
            try
            {
                //在线程里以安全方式调用控件
                if (!control.IsDisposed)
                {
                    if (control.InvokeRequired)
                    {
                        SetUIFlowPanel myInvoke = SetUIFlowPanelControl;
                        if (!control.IsDisposed)
                        {
                            control.Invoke(myInvoke, control, values);
                        }
                    }
                    else
                    {
                        control.Clear();
                        values.ForEach(val =>
                        {
                            control.Add(val);
                        });
                    }
                }
            }
            catch
            { }
        }

        #endregion

        #region 线程 控制 状态 按钮

        /// <summary>
        /// 委托 设置 状态按钮显示
        /// </summary>
        /// <param name="control"></param>
        /// <param name="value"></param>
        private delegate void SetLabelText(UILabel control, string value);

        /// <summary>
        /// 状态按钮显示
        /// </summary>
        public static void SetLabelTextControl(UILabel control, string value)
        {
            try
            {
                //在线程里以安全方式调用控件
                if (!control.IsDisposed)
                {
                    if (control.InvokeRequired)
                    {
                        SetLabelText myInvoke = SetLabelTextControl;
                        if (!control.IsDisposed)
                        {
                            control.Invoke(myInvoke, control, value);
                        }
                    }
                    else
                    {
                        control.Text = value;
                    }
                }
            }
            catch
            { }
        }

        #endregion
    }
}
